package com.mstanford.movie.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mstanford.movie.dao.MovieDao;
import com.mstanford.movie.utils.ConstantUtil;
import com.mstanford.movie.utils.DataUtil;

public class SingleLoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	@SuppressWarnings("unchecked")
	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		Map<String, String> map = null;
		
		HttpSession session = request.getSession();
		Object sessionObj = session.getAttribute("member");
		System.out.println(sessionObj);
		// 从Session中取值
		if(!DataUtil.isEmpty(sessionObj)) {
			map = (Map<String, String>)sessionObj;
		} else {
			String memberEmail = null; // 登录账户
			String memberPwd = null; // 登录密码
			String auto = "false"; // 是否自动登录
			
			// 从Cookie中提取登录信息
			Cookie[] cookies = request.getCookies();
			
			for(Cookie cookie : cookies) {
				if("movieInfo".equals(cookie.getName())) {
					String value = URLDecoder.decode(cookie.getValue(), "UTF-8");
					String[] arr = value.split(",");
					memberEmail = arr[0];
					memberPwd = arr[1];
					
					break;
				}
			}

			// 没有Cookie信息则通过登录页面登录
			if(DataUtil.isEmpty(memberEmail) || DataUtil.isEmpty(memberPwd)) {
				memberEmail = request.getParameter("memberEmail");
				memberPwd = request.getParameter("memberPwd");
				auto = request.getParameter("auto");
			}

			map = new HashMap<String, String>();
			map.put("memberEmail", memberEmail);
			map.put("memberPwd", memberPwd);
			map.put("auto", auto);
			
			// 查询是否有该用户
			MovieDao dao = new MovieDao();
			dao.querySingleMember(map);
			String hasMember = map.get("hasMember"); // 有用户hasMember则为true

			
			if("true".equals(hasMember)) { // 有该用户
				// 判断是否已经登录
				ServletContext context = this.getServletContext();
				Object obj = context.getAttribute(memberEmail);
				
				if(!DataUtil.isEmpty(obj)) {
					map.put("logined", "true");
				} else {
					session.setAttribute("member", map);
				}

				if("true".equals(auto)) { // 勾选自动登录，则将用户信息保存到Cookie中，并将Cookie存储到客户端
					Cookie cookie = new Cookie("movieInfo", URLEncoder.encode(memberEmail + "," + memberPwd, "UTF-8"));
					cookie.setMaxAge(ConstantUtil.MONTH);
					response.addCookie(cookie);
				}
			}
		}

		// 返回Json格式信息
		String json = DataUtil.mapToJson(map);
		System.out.println(map.get("hasMember"));
		System.out.println(json);
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		out.print(json);
		out.flush();
		out.close();
	}
}